# Copyright 2014-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@gnu.org

# This file is part of the gdb testsuite.


if { ![istarget i?86-*-*] && ![istarget x86_64-*-* ] } {
    verbose "Skipping x86 AVX512 tests."
    return
}

standard_testfile

set comp_flags "-mavx512f -I${srcdir}/../nat"

if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
     [list debug nowarnings additional_flags=${comp_flags}]] } {
    return -1
}

if ![runto_main] {
    untested "could not run to main"
    return -1
}

set supports_avx512 0

set test "probe AVX512 support"
gdb_test_multiple "print have_avx512()" $test {
    -re ".. = 1\r\n$gdb_prompt $" {
        pass $test
        set supports_avx512 1
    }
    -re ".. = 0\r\n$gdb_prompt $" {
        pass $test
    }
}

if { !$supports_avx512 } {
    unsupported "processor does not support AVX512"
    return
}

gdb_test "break [gdb_get_line_number "first breakpoint here"]" \
    "Breakpoint .* at .*i386-avx512.c.*" \
    "set first breakpoint in main"
gdb_continue_to_breakpoint "continue to first breakpoint in main"

set nr_regs 8

for { set r 1 } { $r < $nr_regs } { incr r } {
    gdb_test "print/x \$k$r" \
        ".. = 0x[format %x $r]2[format %x $r]1" \
        "check contents of %k$r"
}

for { set r 1 } { $r < $nr_regs } { incr r } {
    gdb_test "set var \$k$r = 0x$r$r$r$r" "" "set %k$r"
}

gdb_test "break [gdb_get_line_number "second breakpoint here"]" \
    "Breakpoint .* at .*i386-avx512.c.*" \
    "set second breakpoint in main"
gdb_continue_to_breakpoint "continue to second breakpoint in main"

set nr_regs 7

for { set r 0 } { $r < $nr_regs } { incr r } {
    set val [expr $r + 1]
    gdb_test "print/x k_data\[$r\]" \
        ".. = 0x$val$val$val$val" \
        "check contents of k_data\[$r\]"
}

gdb_test "break [gdb_get_line_number "third breakpoint here"]" \
    "Breakpoint .* at .*i386-avx512.c.*" \
    "set third breakpoint in main"
gdb_continue_to_breakpoint "continue to third breakpoint in main"

if [is_amd64_regs_target] {
    set nr_regs 8
} else {
    set nr_regs 32
}

for { set r 0 } { $r < $nr_regs } { incr r } {
    gdb_test "print \$zmm$r.v8_double" \
        ".. = \\{$r, $r.125, $r.25, $r.375, $r.5, $r.625, $r.75, $r.875\\}.*" \
        "check double contents of %zmm$r"
    gdb_test "print \$zmm$r.v32_int16" \
        ".. = \\{(-?\[0-9\]+, ){31}-?\[0-9\]+\\}.*" \
        "check int16 contents of %zmm$r"
    gdb_test "print \$ymm$r.v4_double" \
        ".. = \\{$r, $r.125, $r.25, $r.375\\}.*" \
        "check float contents of %ymm$r"
    gdb_test "print \$ymm$r.v16_int16" \
        ".. = \\{(-?\[0-9\]+, ){15}-?\[0-9\]+\\}.*" \
        "check int16 contents of %ymm$r"
    gdb_test "print \$xmm$r.v2_double" \
        ".. = \\{$r, $r.125\\}.*" \
        "check float contents of %xmm$r"
    gdb_test "print \$xmm$r.v8_int16" \
        ".. = \\{(-?\[0-9\]+, ){7}-?\[0-9\]+\\}.*" \
        "check int16 contents of %xmm$r"
}

for { set r 0 } { $r < $nr_regs } { incr r } {
    gdb_test "set var \$zmm$r.v8_double\[0\] = $r + 10" "" "set %zmm$r.v8_double\[0\]"
    gdb_test "set var \$zmm$r.v8_double\[1\] = $r + 10.125" "" "set %zmm$r.v8_double\[1\]"
    gdb_test "set var \$zmm$r.v8_double\[2\] = $r + 10.25" "" "set %zmm$r.v8_double\[2\]"
    gdb_test "set var \$zmm$r.v8_double\[3\] = $r + 10.375" "" "set %zmm$r.v8_double\[3\]"
    gdb_test "set var \$zmm$r.v8_double\[4\] = $r + 10.5" "" "set %zmm$r.v8_double\[4\]"
    gdb_test "set var \$zmm$r.v8_double\[5\] = $r + 10.625" "" "set %zmm$r.v8_double\[5\]"
    gdb_test "set var \$zmm$r.v8_double\[6\] = $r + 10.75" "" "set %zmm$r.v8_double\[6\]"
    gdb_test "set var \$zmm$r.v8_double\[7\] = $r + 10.875" "" "set %zmm$r.v8_double\[7\]"
}

gdb_test "break [gdb_get_line_number "fourth breakpoint here"]" \
    "Breakpoint .* at .*i386-avx512.c.*" \
    "set fourth breakpoint in main"
gdb_continue_to_breakpoint "continue to fourth breakpoint in main"

for { set r 0 } { $r < $nr_regs } { incr r } {
    gdb_test "print zmm_data\[$r\]" \
        ".. = \\{f = \\{[expr $r + 10], [expr $r.125 + 10], [expr $r.25 + 10], [expr $r.375 + 10], [expr $r.5 + 10], [expr $r.625 + 10], [expr $r.75 + 10], [expr $r.875 + 10]\\}\\}.*" \
        "check contents of zmm_data\[$r\] after writing ZMM regs"
}

for { set r 0 } { $r < $nr_regs } { incr r } {
    gdb_test "set var \$ymm$r.v4_double\[0\] = $r + 20" "" "set %ymm$r.v4_double\[0\]"
    gdb_test "set var \$ymm$r.v4_double\[1\] = $r + 20.125" "" "set %ymm$r.v4_double\[1\]"
    gdb_test "set var \$ymm$r.v4_double\[2\] = $r + 20.25" "" "set %ymm$r.v4_double\[2\]"
    gdb_test "set var \$ymm$r.v4_double\[3\] = $r + 20.375" "" "set %ymm$r.v4_double\[3\]"
}

gdb_test "break [gdb_get_line_number "fifth breakpoint here"]" \
    "Breakpoint .* at .*i386-avx512.c.*" \
    "set fifth breakpoint in main"
gdb_continue_to_breakpoint "continue to fifth breakpoint in main"

for { set r 0 } { $r < $nr_regs } { incr r } {
    gdb_test "print zmm_data\[$r\]" \
        ".. = \\{f = \\{[expr $r + 20], [expr $r.125 + 20], [expr $r.25 + 20], [expr $r.375 + 20], [expr $r.5 + 10], [expr $r.625 + 10], [expr $r.75 + 10], [expr $r.875 + 10]\\}\\}.*" \
        "check contents of zmm_data\[$r\] after writing YMM regs"
}

for { set r 0 } { $r < $nr_regs } { incr r } {
    gdb_test "set var \$xmm$r.v2_double\[0\] = $r + 30" "" "set %xmm$r.v2_double\[0\]"
    gdb_test "set var \$xmm$r.v2_double\[1\] = $r + 30.125" "" "set %xmm$r.v2_double\[1\]"
}

gdb_test "break [gdb_get_line_number "sixth breakpoint here"]" \
    "Breakpoint .* at .*i386-avx512.c.*" \
    "set sixth breakpoint in main"
gdb_continue_to_breakpoint "continue to sixth breakpoint in main"

for { set r 0 } { $r < $nr_regs } { incr r } {
    gdb_test "print zmm_data\[$r\]" \
        ".. = \\{f = \\{[expr $r + 30], [expr $r.125 + 30], [expr $r.25 + 20], [expr $r.375 + 20], [expr $r.5 + 10], [expr $r.625 + 10], [expr $r.75 + 10], [expr $r.875 + 10]\\}\\}.*" \
        "check contents of zmm_data\[$r\] after writing XMM regs"
}
